package com.zc.framework.web.tablib.bean;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.util.TagUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * Created by zc on 2016/11/30.
 */
public class DefineTag extends BodyTagSupport {

    private static Logger logger = LogManager.getLogger(DefineTag.class);

    protected String body = null;
    protected String id = null;
    protected String name = null;
    protected String property = null;
    protected String scope = null;
    protected String toScope = null;
    protected String type = null;
    protected String value = null;

    public DefineTag() {
    }

    @Override
    public int doStartTag() throws JspException {
        return 2;
    }

    @Override
    public int doAfterBody() throws JspException {
        if(this.bodyContent != null) {
            this.body = this.bodyContent.getString();
            if(this.body != null) {
                this.body = this.body.trim();
            }

            if(this.body.length() < 1) {
                this.body = null;
            }
        }

        return 0;
    }

    @Override
    public int doEndTag() throws JspException {
        int n = 0;
        if(this.body != null) {
            ++n;
        }

        if(this.name != null) {
            ++n;
        }

        if(this.value != null) {
            ++n;
        }

        if(n != 1) {
            JspException var6 = new JspException("define.value");
            logger.error(var6.getMessage(),var6);
        } else {
            Object value = this.value;
            if(value == null && this.name != null) {
//                value = TagUtils.lookup(this.pageContext, this.name, this.property, this.scope);
            }

            if(value == null && this.body != null) {
                value = this.body;
            }

            if(value == null) {
                JspException var7 = new JspException("define.null");
                throw var7;
            } else {
                int inScope = 1;

                if(this.toScope != null) {
                    inScope = TagUtils.getScope(this.toScope);
                }

                this.pageContext.setAttribute(this.id, value, inScope);
                return 6;
            }
        }

        return  n;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public String getToScope() {
        return toScope;
    }

    public void setToScope(String toScope) {
        this.toScope = toScope;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
